set more off
set matsize 11000


cd \Data

********************************************************************************
** This code produces the estimates of the regression discontinuity model.
** The estimates below are presented in Tables 2, A4, A5, A6, A8, A9, and
** A11. Note, the standard errors for the results presented in Table 3
** and A8 are produced using the Bootstrap.do code.
********************************************************************************

use Temp/HH_Cooling_Estimates.dta, clear
sort premise_id
merge 1:1 premise_id using premise_characteristics.dta
keep if _merge == 3
drop _merge

** Keep premises that are zoned for single family residences
keep if (zoning == "R-1" | zoning == "RD 1" | zoning == "RD 2" | zoning == "RD 3" | zoning == "RD 4" | zoning == "RD 5" | zoning == "RD-1" | zoning == "RD-2" | zoning == "RD-3" | zoning == "RD-4" | zoning == "RD-5" | zoning == "RD1" | zoning == "RD2" | zoning == "RD3" | zoning == "RD4" | zoning == "RD5" | zoning == "RD5PD")

** Create premise-level controls for bedrooms (6 bins), square-footage (12 bins),
** and an indicator for multiple stories. Note, premises with missing Assessor
** information will be dropped.

drop if bedrooms_num == 0 | bedrooms_num == .
replace bedrooms_num = 6 if bedrooms_num > 6
drop if Resdnc_sqft == 0 | Resdnc_sqft == .
gen sqft = 0
replace sqft = 1 if Resdnc_sqft < 1000
forvalues i = 2/11 {
	replace sqft = `i' if Resdnc_sqft < 1000 + (`i' - 1) * 150 & Resdnc_sqft >= 1000 + (`i' - 2) * 150
}
replace sqft = 12 if Resdnc_sqft >= 2500
gen multi = 0
replace multi = 1 if stories > 1

** Set the baseline household:

fvset base 1977 year_built
fvset base 3 bedrooms_num
fvset base 6 sqft
fvset base 0 electric_heat
fvset base 1 stories
fvset base 0 multi

save Temp/HH_Cooling_Estimates_Merged.dta, replace

********************************************************************************
** Tables 2 and A9: Discontinuous changes in average annual cooling and minimum consumption temperature

use Temp/HH_Cooling_Estimates_Merged.dta, clear
replace cooling_kwh = cooling_kwh/2
keep if year_built >= 1968 & year_built <= 1989
keep if electric_heat == 0
gen years_pre = min(year_built - 1978, 0)
gen years_post = max(year_built - 1978, 0)
gen years_pre2 = years_pre^2
gen years_post2 = years_post^2
gen post = 0
replace post = 1 if year_built >= 1978
gen mean_income2 = mean_income^2

xtset comm_block

quiet: reg cooling_kwh post years_pre years_post i.bedrooms##i.sqft##i.multi if min_temp >=52
matrix define beta = e(b)
matrix define beta_1 = beta[1,1..3]

quiet: reg cooling_kwh post years_pre years_post mean_income mean_income2 i.bedrooms##i.sqft##i.multi if min_temp >=52
matrix define beta = e(b)
matrix define beta_2 = beta[1,1..3]

quiet: reg cooling_kwh post years mean_income mean_income2 i.bedrooms##i.sqft##i.multi if min_temp >=52
matrix define beta = e(b)
matrix define beta_3 = beta[1,1..2]

quiet: xtreg cooling_kwh post years_pre years_post i.bedrooms##i.sqft##i.multi if min_temp >=52, fe
matrix define beta = e(b)
matrix define beta_4 = beta[1,1..3]

quiet: xtreg cooling_kwh post years_pre years_post mean_income mean_income2 i.bedrooms##i.sqft##i.multi if min_temp >=52, fe
matrix define beta = e(b)
matrix define beta_5 = beta[1,1..3]

quiet: xtreg cooling_kwh post years mean_income mean_income2 i.bedrooms##i.sqft##i.multi if min_temp >=52, fe
matrix define beta = e(b)
matrix define beta_6 = beta[1,1..2]

quiet: reg min_temp post years_pre years_post i.bedrooms##i.sqft##i.multi if min_temp >=52 & min_temp <=72
matrix define beta = e(b)
matrix define beta_7 = beta[1,1..3]

quiet: reg min_temp post years_pre years_post mean_income mean_income2 i.bedrooms##i.sqft##i.multi if min_temp >=52 & min_temp <=72
matrix define beta = e(b)
matrix define beta_8 = beta[1,1..3]

quiet: reg min_temp post years mean_income mean_income2 i.bedrooms##i.sqft##i.multi if min_temp >=52 & min_temp <=72
matrix define beta = e(b)
matrix define beta_9 = beta[1,1..2]

quiet: xtreg min_temp post years_pre years_post i.bedrooms##i.sqft##i.multi if min_temp >=52 & min_temp <=72, fe
matrix define beta = e(b)
matrix define beta_10 = beta[1,1..3]

quiet: xtreg min_temp post years_pre years_post mean_income mean_income2 i.bedrooms##i.sqft##i.multi if min_temp >=52 & min_temp <=72, fe
matrix define beta = e(b)
matrix define beta_11 = beta[1,1..3]

quiet: xtreg min_temp post years mean_income mean_income2 i.bedrooms##i.sqft##i.multi if min_temp >=52 & min_temp <=72, fe
matrix define beta = e(b)
matrix define beta_12 = beta[1,1..2]

matrix define coef = (beta_1, beta_2, beta_3, beta_4, beta_5, beta_6, beta_7, beta_8, beta_9, beta_10, beta_11, beta_12)


clear
svmat beta
matrix drop _all

** The point estimates are presented in columns 1-6 of Table 2.

rename beta1 cons_change_1
rename beta2 cons_pre_trend_1
rename beta3 cons_post_trend_1
rename beta4 cons_change_2
rename beta5 cons_pre_trend_2
rename beta6 cons_post_trend_2
rename beta7 cons_change_3
rename beta8 cons_trend_3
rename beta9 cons_change_4
rename beta10 cons_pre_trend_4
rename beta11 cons_post_trend_4
rename beta12 cons_change_5
rename beta13 cons_pre_trend_5
rename beta14 cons_post_trend_5
rename beta15 cons_change_6
rename beta16 cons_trend_6

** The point estimates are presented in columns 1-6 of Table A9.

rename beta17 min_change_1
rename beta18 min_pre_trend_1
rename beta19 min_post_trend_1
rename beta20 min_change_2
rename beta21 min_pre_trend_2
rename beta22 min_post_trend_2
rename beta23 min_change_3
rename beta24 min_trend_3
rename beta25 min_change_4
rename beta26 min_pre_trend_4
rename beta27 min_post_trend_4
rename beta28 min_change_5
rename beta29 min_pre_trend_5
rename beta30 min_post_trend_5
rename beta31 min_change_6
rename beta32 min_trend_6


********************************************************************************
** Table A4: Robustness of RD estimates to varying bandwidths and quadratic trends

use Temp/HH_Cooling_Estimates_Merged.dta, clear
replace cooling_kwh = cooling_kwh/2
keep if electric_heat == 0
gen years_pre = min(year_built - 1978, 0)
gen years_post = max(year_built - 1978, 0)
gen years_pre2 = years_pre^2
gen years_post2 = years_post^2
gen post = 0
replace post = 1 if year_built >= 1978
gen mean_income2 = mean_income^2

xtset comm_block

* Linear trends / 1971-1986
reg cooling_kwh post years_pre years_post i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1971 & year_built <= 1986, vce(cluster block_group)
* Linear trends / 1968-1989
reg cooling_kwh post years_pre years_post i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1968 & year_built <= 1989, vce(cluster block_group)
* Linear trends / 1965-1992
reg cooling_kwh post years_pre years_post i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1965 & year_built <= 1992, vce(cluster block_group)
* Quadratic trends / 1971-1986
reg cooling_kwh post years_pre years_post years_pre2 years_post2 i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1971 & year_built <= 1986, vce(cluster block_group)
* Quadratic trends / 1968-1989
reg cooling_kwh post years_pre years_post years_pre2 years_post2 i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1968 & year_built <= 1989, vce(cluster block_group)
* Quadratic trends / 1965-1992
reg cooling_kwh post years_pre years_post years_pre2 years_post2 i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1965 & year_built <= 1992, vce(cluster block_group)


********************************************************************************
** Table A5: RD estimates of change in aggregate annual consumption

use Temp/HH_Cooling_Estimates_Merged.dta, clear
replace total_kwh = total_kwh/2
keep if electric_heat == 0
gen years_pre = min(year_built - 1978, 0)
gen years_post = max(year_built - 1978, 0)
gen years_pre2 = years_pre^2
gen years_post2 = years_post^2
gen post = 0
replace post = 1 if year_built >= 1978
gen mean_income2 = mean_income^2

xtset comm_block

* Linear trends / 1971-1986
reg total_kwh post years_pre years_post i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1971 & year_built <= 1986, vce(cluster block_group)
* Linear trends / 1968-1989
reg total_kwh post years_pre years_post i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1968 & year_built <= 1989, vce(cluster block_group)
* Linear trends / 1965-1992
reg total_kwh post years_pre years_post i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1965 & year_built <= 1992, vce(cluster block_group)
* Quadratic trends / 1971-1986
reg total_kwh post years_pre years_post years_pre2 years_post2 i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1971 & year_built <= 1986, vce(cluster block_group)
* Quadratic trends / 1968-1989
reg total_kwh post years_pre years_post years_pre2 years_post2 i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1968 & year_built <= 1989, vce(cluster block_group)
* Quadratic trends / 1965-1992
reg total_kwh post years_pre years_post years_pre2 years_post2 i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1965 & year_built <= 1992, vce(cluster block_group)


********************************************************************************
** Table A6: RD estimates of change in average annual minimum consumption

use Temp/HH_Cooling_Estimates_Merged.dta, clear
keep if electric_heat == 0
gen years_pre = min(year_built - 1978, 0)
gen years_post = max(year_built - 1978, 0)
gen years_pre2 = years_pre^2
gen years_post2 = years_post^2
gen post = 0
replace post = 1 if year_built >= 1978
gen mean_income2 = mean_income^2

xtset comm_block

* Linear trends / 1971-1986
reg min_kwh post years_pre years_post i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1971 & year_built <= 1986, vce(cluster block_group)
* Linear trends / 1968-1989
reg min_kwh post years_pre years_post i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1968 & year_built <= 1989, vce(cluster block_group)
* Linear trends / 1965-1992
reg min_kwh post years_pre years_post i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1965 & year_built <= 1992, vce(cluster block_group)
* Quadratic trends / 1971-1986
reg min_kwh post years_pre years_post years_pre2 years_post2 i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1971 & year_built <= 1986, vce(cluster block_group)
* Quadratic trends / 1968-1989
reg min_kwh post years_pre years_post years_pre2 years_post2 i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1968 & year_built <= 1989, vce(cluster block_group)
* Quadratic trends / 1965-1992
reg min_kwh post years_pre years_post years_pre2 years_post2 i.bedrooms##i.sqft##i.multi if min_temp >=52 & year_built >= 1965 & year_built <= 1992, vce(cluster block_group)



********************************************************************************
** Table A8: Testing for discontinuities in premise characteristics


use Temp/HH_Cooling_Estimates_Merged.dta, clear
keep if electric_heat == 0
gen years_pre = min(year_built - 1978, 0)
gen years_post = max(year_built - 1978, 0)
gen post = 0
replace post = 1 if year_built >= 1978

reg Resdnc_sqft post years_pre years_post if min_temp >=52, vce(cluster block_group)
matrix define beta = e(b)
scalar define beta_sqft = beta[1,1]
matrix define var = e(V)
scalar define var = var[1,1]
scalar define s_sqft = sqrt(var)

quiet: reg bedrooms post years_pre years_post if min_temp >=52, vce(cluster block_group)
matrix define beta = e(b)
scalar define beta_rooms = beta[1,1]
matrix define var = e(V)
scalar define var = var[1,1]
scalar define s_rooms = sqrt(var)

quiet: reg multi post years_pre years_post if min_temp >=52, vce(cluster block_group)
matrix define beta = e(b)
scalar define beta_multi = beta[1,1]
matrix define var = e(V)
scalar define var = var[1,1]
scalar define s_multi = sqrt(var)

quiet: reg central_ac post years_pre years_post if min_temp >=52, vce(cluster block_group)
matrix define beta = e(b)
scalar define beta_ac = beta[1,1]
matrix define var = e(V)
scalar define var = var[1,1]
scalar define s_ac = sqrt(var)

matrix define beta = (beta_sqft, beta_rooms, beta_multi, beta_ac)
matrix define s = (s_sqft, s_rooms, s_multi, s_ac)

matrix list beta
matrix list s


********************************************************************************
** Table A11: RD estimates -- 1975 through 1982 premises

use Temp/HH_Cooling_Estimates_Merged.dta, clear
replace cooling_kwh = cooling_kwh/2
keep if year_built >= 1975 & year_built <= 1982
keep if electric_heat == 0
gen years_pre = min(year_built - 1978, 0)
gen years_post = max(year_built - 1978, 0)
gen years_pre2 = years_pre^2
gen years_post2 = years_post^2
gen post = 0
replace post = 1 if year_built >= 1978
gen mean_income2 = mean_income^2

xtset comm_block

* Linear Trends / Without 1978-79
reg cooling_kwh post years_pre years_post i.bedrooms##i.sqft##i.multi mean_income mean_income2 if min_temp >=52 & year_built != 1978 & year_built != 1979, vce(cluster block_group)
* Linear Trends / With 1978-79
reg cooling_kwh post years_pre years_post i.bedrooms##i.sqft##i.multi mean_income mean_income2 if min_temp >=52, vce(cluster block_group)
* Without Trends / Without 1978-79
reg cooling_kwh post i.bedrooms##i.sqft##i.multi mean_income mean_income2 if min_temp >=52 & year_built != 1978 & year_built != 1979, vce(cluster block_group)
* Without Trends / With 1978-79
reg cooling_kwh post i.bedrooms##i.sqft##i.multi mean_income mean_income2 if min_temp >=52, vce(cluster block_group)
